package com.crazypos.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazypos.common.ApiRestResponse;
import com.crazypos.common.utils.SecurityUtil;
import com.crazypos.pojo.MenuEntity;
import com.crazypos.pojo.PermissionEntity;
import com.crazypos.pojo.RoleEntity;
import com.crazypos.pojo.bo.CreateRoleBO;
import com.crazypos.pojo.bo.DeleteRoleBO;
import com.crazypos.pojo.bo.UpdateRoleBO;
import com.crazypos.pojo.vo.RoleAndPermissionVO;
import com.crazypos.pojo.vo.RoleDetailVO;
import com.crazypos.security.entity.UserDetailEntity;
import com.crazypos.service.IMenuService;
import com.crazypos.service.IPermissionService;
import com.crazypos.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/menu_list")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/menu_list','SYS:CREATE_ROLE') or hasPermission('/role/menu_list','SYS:UPDATE_ROLE')")
    public ApiRestResponse getMenusList() {
        List<MenuEntity> result = menuService.getAllMenu(null, 1, null);
        return ApiRestResponse.success(result);
    }

    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/list','SYS:QUERY_ROLE')")
    public ApiRestResponse getRolesList(@DefaultValue("1") Integer startPage,
                                        @DefaultValue("10") Integer pageSize,
                                        @DefaultValue("") String name) {
        String currentOrganizationId = SecurityUtil.getOrganizationId();
        IPage<RoleEntity> result = roleService.getRolesList(startPage, pageSize, name, currentOrganizationId);
        return ApiRestResponse.success(result);
    }

    @GetMapping("/detail")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/list','SYS:QUERY_ROLE')")
    public ApiRestResponse getRoleByRoleId(@RequestParam("roleId") String roleId) {
        String currentOrganizationId = SecurityUtil.getOrganizationId();
        RoleDetailVO roleDetail = roleService.getRoleByRoleId(currentOrganizationId, roleId);
        return ApiRestResponse.success(roleDetail);
    }

    @GetMapping("/list2")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/list','SYS:QUERY_ROLE')")
    public ApiRestResponse getRolesList2(@DefaultValue("1") Integer startPage,
                                         @DefaultValue("10") Integer pageSize,
                                         @DefaultValue("") String name) {
        String currentOrganizationId = SecurityUtil.getOrganizationId();
        IPage<RoleAndPermissionVO> result = roleService.getRolesWithPermissions(startPage, pageSize, name, currentOrganizationId);
        return ApiRestResponse.success(result);
    }

    @PostMapping("/create")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/create','SYS:CREATE_ROLE')")
    public ApiRestResponse createRole(@RequestBody @Validated CreateRoleBO bo) {
        UserDetailEntity userInfo = SecurityUtil.getUserInfo();
        roleService.createRole(bo, userInfo.getId(), userInfo.getOrganizationId());
        return ApiRestResponse.success();
    }

    @PostMapping("/update")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/update','SYS:UPDATE_ROLE')")
    public ApiRestResponse updateRole(@RequestBody @Validated UpdateRoleBO bo) {
        UserDetailEntity userInfo = SecurityUtil.getUserInfo();
        roleService.updateRole(bo, userInfo.getId(), userInfo.getOrganizationId());
        return ApiRestResponse.success();
    }

    @PostMapping("/delete")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/delete','SYS:DELETE_ROLE')")
    public ApiRestResponse deleteRole(@RequestBody @Validated DeleteRoleBO bo) {
        UserDetailEntity userInfo = SecurityUtil.getUserInfo();
        roleService.deleteRole(bo.getRoleId(), userInfo.getOrganizationId());
        return ApiRestResponse.success();
    }

    @GetMapping("/all_permissions")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/all_permissions', 'SYS:QUERY_ROLE')")
    public ApiRestResponse getAllPermissions() {
        List<PermissionEntity> permissionEntities = permissionService.getAllPermissions();
        return ApiRestResponse.success(permissionEntities);
    }


    @GetMapping("/all_menus")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/role/all_menus', 'SYS:QUERY_ROLE')")
    public ApiRestResponse getAllMenus() {
        List<MenuEntity> menuEntities = menuService.getAllMenus();
        return ApiRestResponse.success(menuEntities);
    }
}
